package com.optimus.db;

import java.net.MalformedURLException;
import java.net.URL;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author vperez
 */
public class DBFactory {

    private static DBFactory instance = null;
    private SessionFactory factory = null;

    public static DBFactory instance() throws Exception {
        return instance == null ? new DBFactory() : instance;
    }

    private DBFactory() {
        instance=this;
    }

    public SessionFactory currentFactory() throws MalformedURLException {
        if (factory != null) {
            return factory;
        }
        String hibernateConfigFile = "C:\\D\\crawlermetric\\web\\WEB-INF\\hibernate.pg.cfg.xml";
        URL archivo = getClass().getClassLoader().getResource(hibernateConfigFile);
        if (archivo == null) {
            archivo = new URL("file:/" + hibernateConfigFile);
        }

        factory = new Configuration().configure(archivo).buildSessionFactory();
        return factory;
    }
    
    public SessionFactory currentFactory(URL url) throws MalformedURLException {
        if (factory != null) {
            return factory;
        }
        factory = new Configuration().configure(url).buildSessionFactory();
        return factory;
    }
}
